import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PubSub from 'pubsub-js';
import { HashRouter as Router, Route, Link, NavLink, Switch, Redirect } from 'react-router-dom'
import App from './App';
import { RegisterForm } from './components/Register'
import { LoginForm } from './components/Login';
import Home from './components/Home';

// 判断是否存在已登录状态
const requireAuth =(nextState, replace)=>{
  if(!(sessionStorage.getItem("logined"))){
      // Redirect to Home page if not login
      replace({ pathname: '/' })
  }
};


ReactDOM.render(
    <Router>
        <Switch>
            <App>
                <Route exact path="/" component={ LoginForm }/>
                <Route path="/register" component={ RegisterForm }/>
                <Route onEnter={ requireAuth } path="/home" component={ Home }/>
            </App>
        </Switch>
    </Router>
    ,document.getElementById('root')
);
